<?php

class OrdCommentController extends MemberBaseController
{

	/**
	 * 创建评论
	 */
	public function actionCreate($id)
	{
		//取订单
		$order          = Order::model()->findByPk($id);
				
		if($_POST){
			$rate = $_POST['rate'];
			$comment = $_POST['comment'];
			//订单评价
			$ordComment = new OrdComment();
			$ordComment->zo_id = $id;
			$ordComment->zoc_totype = 1;
			$ordComment->zm_id = $order->zm_id_cmp;
			$ordComment->zm_id_sent = Yii::app()->user->getId();
			$ordComment->zoc_type = 2;
			$ordComment->inputtime = time();
			$ordComment->zoc_product = $rate['zoc_product'];
			$ordComment->zoc_service = $rate['zoc_service'];
			$ordComment->zoc_express = $rate['zoc_express'];
			$ordComment->zoc_composite = round(array_sum($rate)/count($rate));//综合评分，取平均值
			$ordComment->updatetime = time();
			$ordComment->save();
			
			//订单商品评论
			foreach($comment as $key=>$val){
				$model = new OrdComment();
				//订单产品
				$ordProduct = ordProduct::model()->findByPk($key);
				$model->zo_id = $id;
				$model->zop_id = $key;
				$model->zp_id = $ordProduct->zp_id;//商品ID
				$model->zoc_totype = 2;
				$model->zm_id = $order->zm_id_cmp;
				$model->zm_id_sent = Yii::app()->user->getId();
				$model->zm_id = $order->zm_id_cmp;
				$model->zoc_spe = $ordProduct->zop_spe;//商品规格
				$model->inputtime = time();
				$model->zoc_sum = $val['zoc_sum'];
				$model->zoc_content = $val['zoc_content'];
				$model->updatetime = time();
				$model->save();
			}

			//写入订单评论状态
			$order->zo_comment = 2;
			$order->zo_voer_time = time();
			if($order->save()){
				//成功跳转
				$this->render('../index/success',array(
						'message'=>'评价成功!',
						'links'=>array(
								array('查看已买到的宝贝','/member/index/orders/'),
						),
				));
				exit;
			}
		}
		$this->render('create',array(
				'order'=>$order,
		));
	}

	/**
	 * 修改评论
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		//取订单
		$order          = Order::model()->findByPk($id);
				
		if($_POST){
			$rate = $_POST['rate'];
			$comment = $_POST['comment'];
			//订单评价
			$ordComment = OrdComment::model()->findByAttributes(array('zo_id'=>$id,'zoc_totype'=>1));
			if(empty($ordComment)){
				$ordComment = new OrdComment();
				$ordComment->zo_id = $id;
				$ordComment->zoc_totype = 1;
				$ordComment->zm_id = $order->zm_id_cmp;
				$ordComment->zm_id_sent = Yii::app()->user->getId();
				$ordComment->zoc_type = 2;
				$ordComment->inputtime = time();
			}
			$ordComment->zoc_product = $rate['zoc_product'];
			$ordComment->zoc_service = $rate['zoc_service'];
			$ordComment->zoc_express = $rate['zoc_express'];
			$ordComment->zoc_composite = round(array_sum($rate)/count($rate));//综合评分，取平均值
			$ordComment->updatetime = time();
			$ordComment->save();

			//订单商品评论
			foreach($comment as $key=>$val){
				$model = OrdComment::model()->findByAttributes(array('zop_id'=>$key,'zoc_totype'=>2));
				if(empty($model)){
					$model = new OrdComment();
					//订单产品
					$ordProduct = ordProduct::model()->findByPk($key);
					$model->zo_id = $id;
					$model->zop_id = $key;
					$model->zp_id = $ordProduct->zp_id;//商品ID
					$model->zoc_totype = 2;
					$model->zm_id = $order->zm_id_cmp;
					$model->zm_id_sent = Yii::app()->user->getId();
					$model->zm_id = $order->zm_id_cmp;
					$model->zoc_spe = $ordProduct->zop_spe;//商品规格
					$model->inputtime = time();
				}
				$model->zoc_sum = $val['zoc_sum'];
				$model->zoc_content = $val['zoc_content'];
				$model->updatetime = time();
				$model->save();
			}

			//写入订单评论状态
			$order->zo_comment = 2;
			$order->zo_voer_time = time();
			if($order->save()){
				//成功跳转
				$this->render('../index/success',array(
						'message'=>'修改评价成功!',
						'links'=>array(
								array('查看已买到的宝贝','/member/index/orders/'),
						),
				));
				exit;
			}
		}
		$this->render('update',array(
				'order'=>$order,
		));
	}
	
	/**
	 *单条，批量删除
	 *传入参数，$GET单条，$POST批量
	 */
	public function actionDelete(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}

		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addInCondition("zoc_id",(array)$id);

		//执行操作
		if(OrdComment::model()->deleteAll($criteria)){
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
	}
	
	/**
	 * 评价管理
	 */
	public function actionAdmin()
	{
	/*	$model=new OrdComment('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['OrdComment']))
			$model->attributes=$_GET['OrdComment'];

		$this->render('admin',array(
			'model'=>$model,
		));*/
		$level = Yii::app()->request->getParam('level', 0);
		$conments = OrdComment::model()->getComments(Yii::app()->user->userid, 1, 1, $level);
		$credit = OrdComment::model()->getAmountOfLevels(Yii::app()->user->userid, 1);
		
		$model = new OrdComment();
		$model->unsetAttributes();

		if(isset($_GET['OrdComment']))
			$model->attributes=$_GET['OrdComment'];
		
		$this->render('admin',array(
				'credit'=>$credit,
				'comments'=>$conments['comments'],
				'pager'=>$conments['pager'],
				"model" => $model,
		));
	}

	/**
	 * 根据主键，载入模型，如果找不到相关数据，跳转到404页面
	 * @param integer the ID 
	 */
	public function loadModel($id)
	{
		$model=OrdComment::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * AJAX验证
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='ord-comment-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
